function getRandomNum() {
    return new Date().getTime() + Math.random() * 1000000000 + "" + Math.random() * 1000000000;
}
function createXHR(req) {
    var xhr = false;
    if (window.XMLHttpRequest) {
        //在IE7+，firefox，Mozilla浏览器中被支持
        xhr = new XMLHttpRequest();
    } else {
        //在IE5.5,IE6中被支持
        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (xhr) {
        var beginTime=new Date().getTime;
        //如果是异步则注册回调函数
        if(req.async){
            xhr.onreadystatechange = function() {
                 if(req.timeout>0){
                          if(new Date().getTime-beginTime>=req.timeout){
                                req.error(xhr, xhr.status,"对不起，链接超时");
                          }
                    }
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        req.success(xhr, xhr.responseText, xhr.status);
                    } else {
                        req.error(xhr, xhr.status);
                    }
                }
            };
        }else{//若是同步，则周期性检测
            var xhrmonitor=window.setInterval(function(){
                 if(req.timeout>0){
                              if(new Date().getTime-beginTime>=req.timeout){
                                    req.error(xhr, xhr.status,"对不起，链接超时");
                                    window.clearInterval(xhrmonitor);
                              }
                        }
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            req.success(xhr, xhr.responseText, xhr.status);
                        } else {
                            req.error(xhr, xhr.status);
                        }
                         window.clearInterval(xhrmonitor);
                    }
            },50);
        }
        var poststr="";
        for(var key in req.data){
             if(poststr==""){
                 poststr=key+"="+encodeURI(req.data[key]);
             }else{
                  poststr+="&"+key+"="+encodeURI(req.data[key]);
             }
        }
        xhr.open(req.type,req.url,req.async,"","");
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhr.send(poststr);
         try {
                    if(req.redirecttype==0){
                        xhr.setRequestHeader("Ajax_Redirect_Type",0);
                    }
                    // Set header so the called script knows that it's an XMLHttpRequest
                    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

                    // Set the Accepts header for the server, depending on the dataType
                    if(req.accepts){
                    xhr.setRequestHeader("Accept", req.dataType && req.accepts[ req.dataType ] ?req.accepts[ req.dataType ] + ", */*" :req.accepts._default );
                    }
         } catch(e){}
    } else {
        alert("浏览器不支持AJAX，请升级");
    }
}
/**
 * ajax函数
 * @param theurl 目标url
 * @param thecallback 回调函数
 * @param isAsync 是否已被0-同步，1-异步
 * @param thetype GET或post
 * @param thedata post参数
 */
function ajax(theurl,thecallback,isAsync,thetype,thedata) {
        if(theurl.indexOf("?")==-1){
            theurl+="?getRandomNum="+getRandomNum();
        }else{
            theurl+="&getRandomNum="+getRandomNum();
        }
        createXHR({
            url: theurl,
            type: thetype,
            dataType: 'XML',
            async:isAsync,
            timeout: 20000,
            data:thedata,
            error: function(xhr, status, e ){
                thecallback("ajax_EXCEPTION");
            },
            success:function(xhr,data,status){
                    thecallback(data);
            }
        });

    }
/**
 * 获取函数的函数名
 * @param call
 */
function getFunctionName(call){
  var func=call+"";
  func=func.substring(0,func.indexOf("("));
  func=func.substring("function".length);
  func=trim(func);
  return func;
}
/**
 * 去空格函数
 * @param str
 */
function trim(str)
{
    return (str || "").replace( /^\s+|\s+$/g, "" );
}
/**
 * 获取事件对象
 */
function getEvent() {
    var ev = window.event;
    if (!ev) {
        var c = this.getEvent.caller;
        while (c) {
            ev = c.arguments[0];
            if (ev && Event == ev.constructor)
                break;
            c = c.caller;
        }
    }
    return ev;
}
/**
 * 获取浏览器类型
 */
function getBrowserType()
{
    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    //alert(browser);
    if ((browser == "Netscape"))
    {
        return "fox";
    }
    else if (browser == "Microsoft Internet Explorer")
    {
        if (version >= 4)
        {
            return "ie4+";
        }
        else
        {
            return "ie4-";
        }
    }
    else
    {
        return "NSupport";
    }
}

//设置表单中组件的回车事件
setEnterKeyEvent=function (id){
    get(id).onkeydown=function(event){
        if(event==undefined){
            event=window.event;
        }
        if(event.keyCode==13){
            this.form.submit();
        }
    }
}

/*****************************cookie************************************
 *
 */
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 30; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function get(id){
    return document.getElementById(id);
}
